package entity

import (
	"encoding/json"
	"log/slog"

	"github.com/lab-online/internal/lab/constant"
	"github.com/lab-online/internal/shared/valueobject"
	"github.com/lab-online/pkg/util"
)

func (l *Lab) ApplyContentPatch(patch []byte) error {
	if modify, err := util.ApplyJSONPatch([]byte(l.Content), patch); err != nil {
		slog.Error(
			"ApplyContentPatch",
			slog.String("patch", string(patch)),
			slog.String("content", string(patch)),
		)
		return constant.RESTErrJSONPatch
	} else {
		l.Content = string(modify)
		return nil
	}
}

func (l *Lab) GetEncodedArguments() []byte {
	v, _ := json.Marshal(&struct {
		ReportID uint `json:"id"`
		Args     any  `json:"args"`
	}{l.ID, l.Arguments})
	return v
}

type LabResponseBody struct {
	ReportID uint `json:"id"`
	Content  any  `json:"content"`
}

func (l *Lab) GetRespBody() *LabResponseBody {
	return &LabResponseBody{l.ID, l.Content}
}

func (l *Lab) MergeReport(reportVO valueobject.ReportVO) {
	if reportVO == nil {
		return
	}

	l.ID = reportVO.GetID()
	l.Content = reportVO.GetContent()
	l.ClassID = reportVO.GetClassID()
	l.ExperimentID = reportVO.GetExperimentID()
	l.Student = reportVO.GetStudent()
	l.Status = reportVO.GetStatus()
	l.Score = reportVO.GetScore()
}
